package com.xyj.action.user;

import java.util.HashMap;
import java.util.Map;

import com.xyj.domain.TUser;
import com.xyj.pageModel.JsonPageModel;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.xyj.action.base.BaseAction;
import com.xyj.pageModel.User;
import com.xyj.service.UserServiceI;
@Namespace("/")
@Action(value="userAction")
public class UserAction extends BaseAction  implements ModelDriven<User>{
	
	private  static final Logger logger = Logger.getLogger(UserAction.class);
	
	private UserServiceI userService;
    private User user = new User();


	public UserServiceI getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}


    public void regUser(){
    	logger.info(">>>>>>>>>regUser>>>>>>");
        JsonPageModel jsonPageModel = new JsonPageModel();
          try {
              userService.saveUser(user);
              jsonPageModel.setSuccess(true);
              jsonPageModel.setMsg("注册成功");
          }catch (Exception e){
              e.printStackTrace();
              jsonPageModel.setMsg("注册失败");
          }
         writeJson(jsonPageModel);
        logger.info(">>>>>>>>>regUser>>>>>>complete");
    }

    public  void login(){
        logger.info(">>>>>>>>>login>>>>>>");
        JsonPageModel jsonPageModel = new JsonPageModel();
        TUser tuser = null;
        try {
            tuser = userService.login(user);
            if(tuser == null){
                jsonPageModel.setMsg("登陆失败,账号或密码不正确");
            }else{
                jsonPageModel.setSuccess(true);
                jsonPageModel.setMsg("登陆成功");
            }
            ;
        } catch (Exception e) {
            e.printStackTrace();
            jsonPageModel.setMsg(e.getMessage());
        }
        writeJson(jsonPageModel);

        logger.info(">>>>>>>>>login>>>>>>complete");
    }

     @Override
    public User getModel() {
        return user;
    }
}
